/*
 * @Author: your name
 * @Date: 2020-02-21 04:16:27
 * @LastEditTime: 2020-02-21 19:51:52
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: /xdy-wap/plugins/router.js
 */
// import Vue from 'vue'
import { localStorage } from '~/plugins/tools'

export default ({ app, redirect }) => {
    app.router.beforeEach((to, from, next) => {
        if (process.client) {
            const userInfo = localStorage.getItem('userInfo')
            // 登录状态
            if (userInfo && userInfo.token) {
                if (['/auth/login', '/auth/register', '/', '/home'].includes(to.path)) {
                    const { companyInfoState, companyState } = localStorage.getItem('companyAuth')
                    const { orgCategoryId, companyId } = userInfo
                    if (
                        !orgCategoryId ||
                        (orgCategoryId === '4' && (companyId === '0' || !companyId) && !companyInfoState)
                    ) {
                        localStorage.setItem('refresh', 'refresh')
                        next('/welcome')
                    } else if (companyInfoState && !companyState) {
                        localStorage.setItem('refresh', 'refresh')
                        next('/company-auth')
                    } else if (!companyInfoState && companyState) {
                        localStorage.setItem('refresh', 'refresh')
                        next('/settled')
                    } else if (companyInfoState && companyState) {
                        if (to.path === '/home') {
                            next()
                        } else {
                            localStorage.setItem('refresh', 'refresh')
                            next('/home')
                        }
                    } else if (orgCategoryId === '3' || orgCategoryId === '2') {
                        if (to.path === '/home') {
                            next()
                        } else {
                            localStorage.setItem('refresh', 'refresh')
                            next('/home')
                        }
                    } else if (orgCategoryId === '5' || orgCategoryId === '6') {
                        if (to.path === '/home') {
                            next()
                        } else {
                            localStorage.setItem('refresh', 'refresh')
                            next('/home')
                        }
                    } else {
                        localStorage.setItem('refresh', 'refresh')
                        next('/home')
                        // next('/personnel-manage')
                    }
                }
            } else if (!to.path.includes('auth')) {
                // 不是登录状态都去登录
                next('/auth/login')
            }
        }
        next()
    })
}
